﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;

namespace XLZF.MicroServices.Users.Controllers;

/// <summary>
/// 用户管理
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
    private readonly IUserRepository _IUserRepository;

    private readonly IMapper _mapper;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="userRepository"></param>
    /// <param name="mapper"></param>
    public UsersController(IUserRepository userRepository, IMapper mapper)
    {
        _IUserRepository = userRepository;

        _mapper = mapper;
    }

    #region 用户

    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]", Name = "Get")]
    public ActionResult Get()
    {
        List<Clothing_Users> list = _IUserRepository.GetClothingUsers();

        //var values = _mapper.Map<List<ClothingDictViewModel>>(list);

        return Ok(list);
    }

    /// <summary>
    /// GetSearch
    /// </summary>
    /// <param name="Seachval"></param>
    /// <param name="pageIndex"></param>
    /// <param name="PageSize"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]", Name = "GetSearch")]
    [Authorize("Apiscope")] //添加授权特性
    public ActionResult GetSearch(string? Seachval = "", int pageIndex = 1, int PageSize = 10)
    {
        Page<Clothing_Users> pagedata = _IUserRepository.GetClothingPageData(Seachval, false, pageIndex, PageSize);

        //var values = _mapper.Map<Page<Clothing_Users>, Page<ClothingDictViewModel>>(pagedata);

        return Ok(pagedata);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("[action]", Name = "Add")]
    public ActionResult Add(Clothing_Users Clothing_Users)
    {
        Clothing_Users.Status = 1;

        Clothing_Users.CreateBy = "1";

        Clothing_Users.CreateDate = DateTime.Now;

        return Ok(_IUserRepository.AddClothingUsers(Clothing_Users));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="UserID"></param>
    /// <param name="BrandName"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("[action]", Name = "Update")]
    public ActionResult Update(int UserID, string BrandName)
    {
        var obj = _IUserRepository.GetClothingUsersSing(UserID);

        obj.UserName = BrandName;

        obj.UpdateBy = "1";

        obj.UpdateDate = DateTime.Now;

        return Ok(_IUserRepository.EditClothingUsers(obj));
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="UserID"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("[action]", Name = "Delete")]
    public ActionResult Delete(int UserID)
    {
        var obj = _IUserRepository.GetClothingUsersSing(UserID);

        return Ok(_IUserRepository.DelClothingUsers(obj));
    }

    #endregion
}
